﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using LTP.Common;
using System.IO;
using System.Text;

namespace RiskMonitor.Web.DemoSwfUpload
{
    /// <summary>
    /// $codebehindclassname$ 的摘要说明
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    public class ScanPhoteUpload : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            try
            {
                string uploadSavePath = context.Server.MapPath("../") +
                    ConfigHelper.GetConfigString("ProofPath").Replace("~/", "").Replace("/", "\\") +
                    DateTime.Now.Year + "_" + DateTime.Now.Month;
                string returnFilePath = ConfigHelper.GetConfigString("ProofPath") + DateTime.Now.Year + "_" + DateTime.Now.Month;
                if (!System.IO.Directory.Exists(uploadSavePath))
                {
                    System.IO.Directory.CreateDirectory(uploadSavePath);
                }

                HttpPostedFile file;
                for (int i = 0; i < context.Request.Files.Count; ++i)
                {
                    file = context.Request.Files[i];
                    Random ra = new Random();
                    string filetp = Path.GetExtension(file.FileName);
                    string NewfileName = System.DateTime.Now.ToString("yyyyMMddHHmmss") +
                        ra.Next(10, 99).ToString() + filetp.ToString(); //上传后文件名称！ 

                    returnFilePath = returnFilePath + "/" + NewfileName;
                    NewfileName = uploadSavePath + "\\" + NewfileName;
                    if (file == null || file.ContentLength == 0 || string.IsNullOrEmpty(file.FileName)) continue;
                    file.SaveAs(NewfileName);

                    try
                    {
                        GetThumb(file.InputStream, NewfileName.Replace(filetp, "_thumb" + filetp), 800, 600);
                    }
                    catch { }

                    //context.Response.Redirect("/FlexPlayer/FlexPaperViewer.html?SwfFile=" + 
                    //    swfFile.Substring(swfFile.IndexOf("Resource"), swfFile.Length - swfFile.IndexOf("Resource")), false);
                    context.Response.Write("Success#" + returnFilePath);
                    context.ApplicationInstance.CompleteRequest();
                }
            }
            catch (Exception ex)
            {
                context.Response.StatusCode = 500;
                context.Response.Write(ex.Message);
                context.ApplicationInstance.CompleteRequest();
            }
            finally
            {
                context.ApplicationInstance.CompleteRequest();
            }
        }

        private void GetThumb(System.IO.Stream stream, string _strPicThumbSavePath, int _iThumbBigMaxWidth, int _iThumbBigMaxHeight)
        {
            System.Drawing.Image imgOraginal = System.Drawing.Image.FromStream(stream);
            int iOraginalWidth = imgOraginal.Width;
            int iOraginalHeight = imgOraginal.Height;

            int toWidth;
            int toHeight;

            //原图的宽高比
            float fWbH = (float)iOraginalWidth / (float)iOraginalHeight;
            //原图的高宽比
            float fHbW = (float)iOraginalHeight / (float)iOraginalWidth;

            //如果宽高比大于4:3，则按照最大宽度来生成缩略图
            if (fWbH >= (4.0f / 3.0f))
            {
                if (_iThumbBigMaxWidth < iOraginalWidth)
                {
                    toWidth = _iThumbBigMaxWidth;
                    toHeight = (int)(toWidth * fHbW);
                }
                else//如果指定的缩略图宽度大于原图的宽度，则不生成缩略图
                {
                    toWidth = iOraginalWidth;
                    toHeight = iOraginalHeight;
                }
            }
            else//如果宽高比小雨4:3，则按照最大高度来生成缩略图
            {
                if (_iThumbBigMaxWidth < iOraginalWidth)
                {
                    toHeight = _iThumbBigMaxWidth;
                    toWidth = (int)(toHeight * fWbH);
                }
                else //如果指定的缩略图高度小于原图的宽度，则不生成缩略图
                {
                    toWidth = iOraginalWidth;
                    toHeight = iOraginalHeight;
                }
            }

            System.Drawing.Image bitmap = new System.Drawing.Bitmap(toWidth, toHeight);
            System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap);

            //设定缩略图的生成质量
            g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;

            g.Clear(System.Drawing.Color.Transparent);
            g.DrawImage(imgOraginal, new System.Drawing.Rectangle(0, 0, toWidth, toHeight), new System.Drawing.Rectangle(0, 0, iOraginalWidth, iOraginalHeight), System.Drawing.GraphicsUnit.Pixel);

            try
            {
                bitmap.Save(_strPicThumbSavePath);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            finally
            {
                imgOraginal.Dispose();
                bitmap.Dispose();
                g.Dispose();
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}
